<?php
/**
 * sku品质报表模型
 * (model)
 */
namespace App\Models\Quality;

use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class QReport extends Model
{
    protected $table = 'quality_report';

    protected $guarded = [];

    protected $casts = [
        'size' => 'array',
        'produce' => 'array',
        'design' => 'array',
        'subject' => 'array',
    ];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function headersField(){
        $fields = [];
        $fields['report_date'] = ['title' => '日期','sumField' => false];
        $fields['area'] = ['title' => '站点','sumField' => false];
        $fields['country'] = ['title' => '国家','sumField' => false];
        $fields['origin'] = ['title' => '来源','sumField' => false];
        $fields['order_no'] = ['title' => '订单号','sumField' => false];
        $fields['sku'] = ['title' => 'SKU','sumField' => false];
        $fields['sku_cn'] = ['title' => '品名','sumField' => false];
        $fields['season'] = ['title' => '季节性','sumField' => false];
        $fields['classify'] = ['title' => '品类','sumField' => false];
        $fields['sales_num'] = ['title' => '当月销量','sumField' => true];
        $fields['return_num'] = ['title' => '当月退货量','sumField' => true];
        $fields['return_rate'] = ['title' => '当月退货率','sumField' => false];
        $fields['saleable_num'] = ['title' => '可二次销售量','sumField' => true];
        $fields['saleable_rate'] = ['title' => '可二次销售率','sumField' => false];
        $fields['size'] = ['title' => '尺码问题','sumField' => false];
        $fields['produce'] = ['title' => '做工问题','sumField' => false];
        $fields['design'] = ['title' => '设计问题','sumField' => false];
        $fields['subject'] = ['title' => '主观问题','sumField' => false];
        $fields['total_rate'] = ['title' => '汇总问题','sumField' => false];
        return $fields;
    }

    public static function getData($re){
        foreach ($re as $row){
            $sizes = count($row->size ?: []);
            $produces = count($row->produce ?: []);
            $designs = count($row->design ?: []);
            $subjects = count($row->subject ?: []);
            $count = max($sizes,$produces,$designs,$subjects,1);
            $total = 0;
            $sizeArr = self::_getTypeData($total,$row->size ?: [],$row->sales_num);
            $produceArr = self::_getTypeData($total,$row->produce ?: [],$row->sales_num);
            $designArr = self::_getTypeData($total,$row->design ?: [],$row->sales_num);
            $subjectArr = self::_getTypeData($total,$row->subject ?: [],$row->sales_num);

            //客诉
            $QComplaint = QComplaint::query()->where([
                'report_date' => $row->report_date,
                'area_id' => $row->area_id,
                'country_id' => $row->country_id,
                'psku_id' => $row->psku_id,
            ])->first();

            for($i = 0; $i < $count; $i++){
                $temp = [];
                $temp['report_date'] = $row->report_date;
                $temp['area'] = $row->area->area ?? '';
                $temp['country'] = $row->country->country ?? '';
                $temp['origin'] = !empty($QComplaint) ? QComplaint::$_origin[$QComplaint->origin] : '';
                $temp['order_no'] = $QComplaint->order_no ?? '';
                $temp['psku_code'] = $row->sku->psku_code ?? '';
                $temp['psku_cn'] = $row->sku->psku_cn ?? '';
                $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
                $temp['title'] = $row->sku->classify->title ?? '';
                $temp['sales_num'] = $i == 0 ? $row->sales_num : '';
                $temp['return_num'] = $i == 0 ? $row->return_num : '';
                $temp['return_rate'] = $i == 0 ? getPercentage($row->return_num,$row->sales_num) : '';
                $temp['saleable_num'] = $i == 0 ? $row->saleable_num : '';
                $temp['saleable_rate'] = $i == 0 ? getPercentage($row->saleable_num,$row->sales_num) : '';
                $temp['ks'] = $sizeArr[$i]['k'] ?? '';
                $temp['size'] = $sizeArr[$i]['v'] ?? '';
                $temp['size_rate'] = $sizeArr[$i]['rate'] ?? '';
                $temp['kp'] = $produceArr[$i]['k'] ?? '';
                $temp['produce'] = $produceArr[$i]['v'] ?? '';
                $temp['produce_rate'] = $produceArr[$i]['rate'] ?? '';
                $temp['kd'] = $designArr[$i]['k'] ?? '';
                $temp['design'] = $designArr[$i]['v'] ?? '';
                $temp['design_rate'] = $designArr[$i]['rate'] ?? '';
                $temp['ksu'] = $subjectArr[$i]['k'] ?? '';
                $temp['subject'] = $subjectArr[$i]['v'] ?? '';
                $temp['subject_rate'] = $subjectArr[$i]['rate'] ?? '';
                $temp['total_rate'] = $i == 0 ? getPercentage($total,$row->sales_num) : '';
                yield $temp;
            }
        }
    }

    private static function _getTypeData(&$total,$data,$salesNum){
        $arr = [];
        $i = 0;
        foreach($data as $k => $v){
            $total += $v;
            $arr[$i]['k'] = $k;
            $arr[$i]['v'] = $v;
            $arr[$i]['rate'] = getPercentage($v,$salesNum);
            $i++;
        }
        return $arr;
    }
}
